/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author KaiLee
 */
public class connectSQL {
    private Connection conn;
    
    public connectSQL(){
        conn = getConnection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/banhang", "root", "");
    }
    public Connection getConnection(String dbClass, String dbUrl, String user, String pass){
        Connection con = null;
        try{
            Class.forName(dbClass);
            con = DriverManager.getConnection(dbUrl,user,pass);
            
        }catch(Exception ex){
        }
        return con;
    }
    
    public boolean checkAdmin(AdminBean admin){
       
        try {
            String query = "select * from user_ad where username = ? and password = ?";
        
            PreparedStatement ps = (PreparedStatement) conn.prepareStatement(query);
            ps.setString(1, admin.getUserId());
            ps.setString(2, admin.getPassword());
            
            ResultSet rs = ps.executeQuery();
            
            if(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(connectSQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    
    public boolean checkCustomer(String username,String password){
        try {
            String query = "select * from user where username = ? and password = ?";
        
            PreparedStatement ps = (PreparedStatement) conn.prepareStatement(query);
             ps.setString(1,username);
             ps.setString(2,password);
            
            ResultSet rs = ps.executeQuery();
            
            if(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            System.out.println("bun qua");
            Logger.getLogger(connectSQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
}
    
    public boolean insertCustomer(Customer customer){
        try{
            String query = new String("");
        }catch(Exception e){
        Logger.getLogger(e.getMessage());
        }
                
        return false;
    }
    
}
